import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

/**
  * @Description: shiro认证示例
  * @Author: Bink
  * @DateTime: 2021/6/9 7:56 上午
 */
public class HelloShiro {

    @Test
    public void shiroLogin(){
        //导入INI配置
        IniRealm iniRealm = new IniRealm("classpath:shiro.ini");
        //构建安全管理器
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager(iniRealm);
        //使用工具生效安全管理器
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        //使用工具获得subject主体
        Subject subject = SecurityUtils.getSubject();
        //构建账户密码
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("Bink","");
        //使用subject主体去登录
        subject.login(usernamePasswordToken);
        //打印登录信息
        System.out.println("登录结果:"+subject.isAuthenticated());
    }

}
